package com.john.bms.controller.system;

import com.john.bms.config.jwt.JwtConfiguration;
import com.john.bms.controller.BaseController;
import com.john.bms.model.result.RequestResult;
import com.john.bms.model.system.LoginModel;
import com.john.bms.service.system.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static com.john.bms.utils.CollectionUtils.map;


@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController extends BaseController {

    @Resource
    private UserService userService;


    @PostMapping("/doLogin")
    public RequestResult doLogin(@RequestBody LoginModel loginModel) throws Exception {
        String state = userService.doLogin(loginModel);
        if(state != null){
            Map<String, String> token = new HashMap<>(2);
            token.put("token", state);
            return RequestResult.of(map(token, "data")).setCode(20000);
        }

        return null;
    }

    @RequestMapping("/info")
    public RequestResult info(String token) {
        Map<String, Object> map = new HashMap<>(6);
        map.put("code", 20000);
        map.put("data", Arrays.asList("admin"));
        map.put("introduction", "I am a super administrator");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name", "Admin");

        return RequestResult.of(map(map, "data")).setCode(20000);
    }
}
